package com.lekang.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lekang.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;

/**
 * 首页统计Controller
 * 
 * @author lekang
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/system/dashboard")
public class DashboardController extends BaseController
{
    @Autowired
    private ElderlyMapper elderlyMapper;

    @Autowired
    private BedMapper bedMapper;
    
    @Autowired
    private TaskArrangementMapper taskArrangementMapper;
    
    @Autowired
    private PrepaymentRechargeMapper rechargeMapper;

    /**
     * 获取首页统计数据
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 1. 统计老人数量（已入住状态）
        int elderlyCount = elderlyMapper.countElderly();
        data.put("elderlyCount", elderlyCount);
        
        // 2. 统计床位总数
        int totalBeds = bedMapper.countTotalBeds();
        data.put("totalBeds", totalBeds);
        
        // 3. 统计已使用床位数
        int usedBeds = bedMapper.countUsedBeds();
        data.put("usedBeds", usedBeds);
        
        // 4. 统计服务单数量（任务安排）
        int serviceCount = taskArrangementMapper.countTotalTasks();
        data.put("serviceCount", serviceCount);
        
        // 5. 统计员工数量（护理员数量）
        int userCount = taskArrangementMapper.countCaregivers();
        data.put("userCount", userCount);
        
        // 6. 统计总收入（充值金额）
        Double totalIncome = rechargeMapper.sumTotalRecharge();
        data.put("totalIncome", totalIncome != null ? totalIncome : 0.0);
        
        return success(data);
    }

    /**
     * 获取老人等级分布统计
     */
    @GetMapping("/nursing-level-stats")
    public AjaxResult getNursingLevelStats()
    {
        List<Map<String, Object>> stats = elderlyMapper.getNursingLevelStats();
        return success(stats);
    }

    /**
     * 获取老人年龄分布统计
     */
    @GetMapping("/age-stats")
    public AjaxResult getAgeStats()
    {
        List<Map<String, Object>> stats = elderlyMapper.getAgeStats();
        return success(stats);
    }
}

